package com.irdstudio.efp.esb.service.impl.hj;

import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.efp.esb.common.client.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.common.constant.ESBClientConstance;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.service.bo.req.hj.RepAccLoanInfoQueryBean;
import com.irdstudio.efp.esb.service.bo.resp.hj.RespAccLoanInfoQueryBean;
import com.irdstudio.efp.esb.service.client.ESBRequestBuilder;
import com.irdstudio.efp.esb.service.client.EsbBaseBean;
import com.irdstudio.efp.esb.service.facade.hj.AccLoanInfoQueryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import static com.irdstudio.basic.framework.core.constant.ESBConstant.SCENE_NO_01;
import static com.irdstudio.basic.framework.core.constant.ESBConstant.TRADE_CODE_30220010;

/**
 * 需求编号:2020D05--
 * 问题编号:
 * 开发人员: wengqw
 * 创建日期:2020/11/12
 * 功能描述:
 * 修改日期:2020/11/12
 * 修改描述:
 */
@Service("accLoanInfoQueryService")
public class AccLoanInfoQueryServiceImpl implements AccLoanInfoQueryService {

    Logger logger = LoggerFactory.getLogger(AccLoanInfoQueryServiceImpl.class);

    @Override
    public RespAccLoanInfoQueryBean loanInfoQuery(RepAccLoanInfoQueryBean repAccLoanInfoQueryBean, String glblSrlNo) throws Exception{
        RespAccLoanInfoQueryBean respAccLoanInfoQueryBean = null;
        logger.info("======>调用贷款借据信息查询接口【" + glblSrlNo + "_30230010_01】开始<======");
        String retMsg = "";
        try{
            EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                    anEsbBaseBean().
                    withINSTID(ESBClientConstance.bdInstId).withGLBLSRLNO(glblSrlNo).build();

            EsbRespServiceBean recvBean = ESBRequestBuilder.buildClient().
                    withEsbBaseBean(esbBaseBean)
                    .withBody(repAccLoanInfoQueryBean)
                    .withTradeNo(ESBConstant.TRADE_CODE_30230010)
                    .withScene(ESBConstant.SCENE_NO_01)
                    .create()
                    .sendAndReceive();
            String retCd = recvBean.getSysHead().getRetInfArry().get(0).getRetCd();
            retMsg = recvBean.getSysHead().getRetInfArry().get(0).getRetMsg();
            if (EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(retCd)) {
                respAccLoanInfoQueryBean = recvBean.getBody(RespAccLoanInfoQueryBean.class);
            }
            if (null != respAccLoanInfoQueryBean) {
                logger.info("核心客户号" + repAccLoanInfoQueryBean.getCustAcctNo() + "客户借据信息查询成功");
            } else {
                logger.info("核心客户号" + repAccLoanInfoQueryBean.getCustAcctNo() + "对应客户借据信息不存在");
                throw new BizException("查询客户借据信息" + TRADE_CODE_30220010 + SCENE_NO_01 + "接口失败！" + "核心客户号" + repAccLoanInfoQueryBean.getCustAcctNo());
            }
        } catch (Exception e) {
            logger.info("贷款借据信息查询失败！"+retMsg);
            throw e;
        } finally {
            logger.info("======>调用贷款借据信息查询接口【" + glblSrlNo + "_30230010_01】结束<======");
        }
        return respAccLoanInfoQueryBean;
    }
}
